<?php

namespace App\Mobile\Controllers;

include base_path() . '/vendor/autoload.php';

use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use DB;
use Auth;
use Validator;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\User;
use App\Agent;
use App\SuperAgent;
use App\RebateLog;

class WechatController extends Controller
{
	public $config = [
	    'app_id' => 'wxb19dc538de82d329',
	    'secret' => 'a14a0d4744840960f16866056f7da387',
	    'payment' => [
	    	'merchant_id' => '1502450861',
	    	'key' => 'leyouleyouleyouleyouleyouleyou18'
	    ]
	];

	/**
	 * [wechatBack 微信支付异步通知]
	 * @return [type] [description]
	 */
	public function payBack()
	{
	    $wxApp = new Application($this->config);
	    $payment = $wxApp->payment;
	    $response = $payment->handleNotify(function ($notify, $successful){
	        if ($successful) {
	            $order_arr = json_decode($notify, true);
	            $order_sn = $order_arr['out_trade_no'];
	            $order = \App\Order::where('order_sn', '=', $order_sn)->first();
	            if ($order) {
	            	$sale_num = DB::table(DB::raw('orders as o'))->join('order_goods as og', function($join){
	            		$join->on('o.order_sn', '=', 'og.order_sn');
	            	})->join('goods as g', function($join){
	            		$join->on('g.goods_id', '=', 'og.goods_id');
	            	})->select(DB::raw('g.sale_num'))->where('o.order_sn', $order_sn)->value('g.sale_num');
	            	DB::beginTransaction();
	            	$lock_order = DB::table('orders')->where('order_sn', $order_sn)->lockForUpdate()->first();
	            	if ($lock_order->pay_status != 1) {
		            	//返利分成
		            	$user = User::where(['userid' => $order->user_id])->first();
		            	if ($user && $user->daili_id != 0) {
		            		$this->userDivid($user, $order);
		            	}
		            	$user->gems += $sale_num;
		            	$user->save();
	            	}
	            	$order->pay_status = 1;
	            	$order->save();
	            	DB::commit();
	            }
	            return true;
	        }
	    });
	    return $response;
	}

	/**
	 * 代理分成返利
	 */
	public function userDivid($user, $order)
	{
		$parent_daili = Agent::where(['id' => $user->daili_id])->first();
		if ($parent_daili) {
			switch ($parent_daili->level) {
				case 1://二级代理
					if ($parent_daili->superagent_id != 0) {//有一级代理
						$agent_one = SuperAgent::where(['id' => $parent_daili->superagent_id])->first();
						if ($agent_one) {
							$this->dividOne($agent_one, $parent_daili, $order);
						}
					} else {//没有一级代理
						$this->dividTwo($parent_daili, $order);
					}
					break;
				case 2://三级代理
					$agent_two = Agent::where(['id' => $parent_daili->pid])->first();
					if ($agent_two->superagent_id != 0) {
						$agent_one = SuperAgent::where(['id' => $agent_two->superagent_id])->first();
						if ($agent_one) {
							$this->dividThree($agent_one, $agent_two, $parent_daili, $order);
						}
					} else {
						$this->dividFour($agent_two, $parent_daili, $order);
					}
					break;
				case 3://四级代理
					$agent_three = Agent::where(['id' => $parent_daili->pid])->first();
					if ($agent_three) {
						$agent_two = Agent::where(['id' => $agent_three->pid])->first();
						if ($agent_two->superagent_id != 0) {
							$agent_one = SuperAgent::where(['id' => $agent_two->superagent_id])->first();
							if ($agent_one) {
								$this->dividFive($agent_one, $agent_two, $agent_three, $parent_daili, $order);
							}
						} else {
							$this->dividSix($agent_two, $agent_three, $parent_daili, $order);
						}
					}
					break;
			}
		}
		return;
	}

	public function dividOne($agent_one, $agent_two, $order) 
	{
		$fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100);//二级代理分钱
		$fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two;//一级代理分钱
		$agent_one->money += $fee_one;
		$agent_one->save();
		$agent_two->money += $fee_two;
		$agent_two->save();
		$log = new RebateLog;
		$log->order_sn = $order->order_sn;
		$log->type = 1;
		$log->daili_id = $agent_one->id;
		$log->second_id = $agent_two->id;
		$log->user_id = $order->user_id;
		$log->fee = $fee_one;
		$log->save();
		$log_two = new RebateLog;
		$log_two->order_sn = $order->order_sn;
		$log_two->type = 2;
		$log_two->daili_id = $agent_one->id;
		$log_two->second_id = $agent_two->id;
		$log_two->user_id = $order->user_id;
		$log_two->fee = $fee_two;
		$log_two->save();
		return;
	}

	public function dividTwo($agent_two, $order)
	{
		$fee_two = $order->order_amount * ($agent_two->divide / 100);
		$agent_two->money += $fee_two;
		$agent_two->save();
		$log = new RebateLog;
		$log->order_sn = $order->order_sn;
		$log->type = 2;
		$log->second_id = $agent_two->id;
		$log->user_id = $order->user_id;
		$log->fee = $fee_two;
		$log->save();
		return;
	}

	public function dividThree($agent_one, $agent_two, $agent_three, $order)
	{
		$fee_three = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100);
		$fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) - $fee_three;
		$fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two - $fee_three;
		$agent_one->money += $fee_one;
		$agent_one->save();
		$agent_two->money += $fee_two;
		$agent_two->save();
		$agent_three->money += $fee_three;
		$agent_three->save();
		$log_one = new RebateLog;
		$log_one->order_sn = $order->order_sn;
		$log_one->type = 1;
		$log_one->daili_id = $agent_one->id;
		$log_one->second_id = $agent_two->id;
		$log_one->third_id = $agent_three->id;
		$log_one->user_id = $order->user_id;
		$log_one->fee = $fee_one;
		$log_one->save();
		$log_two = new RebateLog;
		$log_two->order_sn = $order->order_sn;
		$log_two->type = 2;
		$log_two->daili_id = $agent_one->id;
		$log_two->second_id = $agent_two->id;
		$log_two->third_id = $agent_three->id;
		$log_two->user_id = $order->user_id;
		$log_two->fee = $fee_two;
		$log_two->save();
		$log_three = new RebateLog;
		$log_three->order_sn = $order->order_sn;
		$log_three->type = 3;
		$log_three->daili_id = $agent_one->id;
		$log_three->second_id = $agent_two->id;
		$log_three->third_id = $agent_three->id;
		$log_three->user_id = $order->user_id;
		$log_three->fee = $fee_three;
		$log_three->save();
		return;
	}

	public function dividFour($agent_two, $agent_three, $order)
	{
		$fee_three = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100);	
		$fee_two = $order->order_amount * ($agent_two->divide / 100) - $fee_three;
		$agent_two->money += $fee_two;
		$agent_two->save();
		$agent_three->money += $fee_three;
		$agent_three->save();
		$log_two = new RebateLog;
		$log_two->order_sn = $order->order_sn;
		$log_two->type = 2;
		$log_two->second_id = $agent_two->id;
		$log_two->third_id = $agent_three->id;
		$log_two->user_id = $order->user_id;
		$log_two->fee = $fee_two;
		$log_two->save();
		$log_three = new RebateLog;
		$log_three->order_sn = $order->order_sn;
		$log_three->type = 3;
		$log_three->second_id = $agent_two->id;
		$log_three->third_id = $agent_three->id;
		$log_three->user_id = $order->user_id;
		$log_three->fee = $fee_three;
		$log_three->save();
		return;
	}

	public function dividFive($agent_one, $agent_two, $agent_three, $agent_four, $order)
	{
		$fee_four = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100) * ($agent_four->divide / 100);
		$fee_three = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100) - $fee_four;
		$fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) - $fee_three - $fee_four;
		$fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two - $fee_three - $fee_four;
		$agent_one->money += $fee_one;
		$agent_one->save();
		$agent_two->money += $fee_two;
		$agent_two->save();
		$agent_three->money += $fee_three;
		$agent_three->save();
		$agent_four->money += $fee_four;
		$agent_four->save();
		$log_one = new RebateLog;
		$log_one->order_sn = $order->order_sn;
		$log_one->type = 1;
		$log_one->daili_id = $agent_one->id;
		$log_one->second_id = $agent_two->id;
		$log_one->third_id = $agent_three->id;
		$log_one->fourth_id = $agent_four->id;
		$log_one->user_id = $order->user_id;
		$log_one->fee = $fee_one;
		$log_one->save();
		$log_two = new RebateLog;
		$log_two->order_sn = $order->order_sn;
		$log_two->type = 2;
		$log_two->daili_id = $agent_one->id;
		$log_two->second_id = $agent_two->id;
		$log_two->third_id = $agent_three->id;
		$log_two->fourth_id = $agent_four->id;
		$log_two->user_id = $order->user_id;
		$log_two->fee = $fee_two;
		$log_two->save();
		$log_three = new RebateLog;
		$log_three->order_sn = $order->order_sn;
		$log_three->type = 3;
		$log_three->daili_id = $agent_one->id;
		$log_three->second_id = $agent_two->id;
		$log_three->third_id = $agent_three->id;
		$log_three->fourth_id = $agent_four->id;
		$log_three->user_id = $order->user_id;
		$log_three->fee = $fee_three;
		$log_three->save();
		$log_four = new RebateLog;
		$log_four->order_sn = $order->order_sn;
		$log_four->type = 4;
		$log_four->daili_id = $agent_one->id;
		$log_four->second_id = $agent_two->id;
		$log_four->third_id = $agent_three->id;
		$log_four->fourth_id = $agent_four->id;
		$log_four->user_id = $order->user_id;
		$log_four->fee = $fee_four;
		$log_four->save();
		return;
	}

	public function dividSix($agent_two, $agent_three, $agent_four, $order)
	{
		$fee_four = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100) * ($agent_four->divide / 100);
		$fee_three = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100) - $fee_four;
		$fee_two = $order->order_amount * ($agent_two->divide / 100) - $fee_three - $fee_four;
		$agent_two->money += $fee_two;
		$agent_two->save();
		$agent_three->money += $fee_three;
		$agent_three->save();
		$agent_four->money += $fee_four;
		$agent_four->save();
		$log_four = new RebateLog;
		$log_four->order_sn = $order->order_sn;
		$log_four->type = 4;
		$log_four->second_id = $agent_two->id;
		$log_four->third_id = $agent_three->id;
		$log_four->fourth_id = $agent_four->id;
		$log_four->user_id = $order->user_id;
		$log_four->fee = $fee_four;
		$log_four->save();
		$log_three = new RebateLog;
		$log_three->order_sn = $order->order_sn;
		$log_three->type = 3;
		$log_three->second_id = $agent_two->id;
		$log_three->third_id = $agent_three->id;
		$log_three->fourth_id = $agent_four->id;
		$log_three->user_id = $order->user_id;
		$log_three->fee = $fee_three;
		$log_three->save();
		$log_two = new RebateLog;
		$log_two->order_sn = $order->order_sn;
		$log_two->type = 2;
		$log_two->second_id = $agent_two->id;
		$log_two->third_id = $agent_three->id;
		$log_two->fourth_id = $agent_four->id;
		$log_two->user_id = $order->user_id;
		$log_two->fee = $fee_two;
		$log_two->save();
		return;
	}

}